#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>


void gemm1(float *A, float *B, float *C, int m, int n, int k){
    for (int i = 0; i < m; i++){
        for (int j = 0; j < n; j++){
            for (int c = 0; c < k; c++){
                C[i*n + j] += A[i*k + c] * B[c*n + j];
            }
        }
    }
}

void gemm2(float *A, float *B, float *C, int m, int n, int k){
    for (int i = 0; i < m; i++){
        for (int c = 0; c < k; c++){
            for (int j = 0; j < n; j++){
                C[i*n + j] += A[i*k + c] * B[c*n + j];
            }
        }
    }
}

int main() {
    int m = 512, n = 512, k = 512;
    float *A = (float *)malloc(m*k*sizeof(float));
    float *B = (float *)malloc(k*n*sizeof(float));
    float *C = (float *)malloc(m*n*sizeof(float));

    for (int i = 0; i < m*k; i++){
        A[i] = 1.0f;
    }

    for (int i = 0; i < k*n; i++){
        B[i] = 1.0f;
    }

    for (int i = 0; i < m*n; i++){
        C[i] = 0.0f;
    }

    DWORD start_t, end_t;

    start_t = GetTickCount();
    
    gemm2(A, B, C, m , n, k);

    end_t = GetTickCount();

    double time = (end_t - start_t)/1e+3;
    printf("time: %lf s\n", time);

    double flops = 2.0 * m * n * k * 1e-9;
    printf("flops: %lf Gflops\n", flops/time);

    free(A);
    free(B);
    free(C);
    return 0;
}